<?php
namespace app\admin\controller;

use think\Controller;
use think\Db;
use think\Session;
class Login extends Controller
{
    public function getlogin()
    {
    	//加载后台模板
    	return $this->fetch("Login/login");
    }

    public function postdologin(){
    	$request=request();
    	$fcode=$request->param('fcode');
    	// echo $fcode;
    	$username=$request->param('username');
    	$password=$request->param('password');
    	// var_dump($password);
    	// var_dump($request->param());exit;
    	if(captcha_check($fcode)){
    		//检测用户名和密码
    		$row=Db::table('admin_users')->where("name='{$username}' and password='{$password}'")->select();
    		if($row){
    			//登录成功,存储在session里
    			Session::set('islogin',2);
    			Session::set('username',$row[0]['name']);
                Session::set('adminsid',$row[0]['id']);
                //获取当前登录管理员拥有的所有权限
                $list=Db::query("select n.name,n.mname,n.aname from user_role as ur,role_node as rn,node as n where ur.rid=rn.rid and rn.nid=n.id and uid={$row[0]['id']}");
                //初始化权限信息
                //让所有管理员都有后台首页访问权限
                $nodelist['admin'][]='getindex';
                //遍历权限
                foreach($list as $key=>$v){
                    $nodelist[$v['mname']][]=$v['aname'];
                    //如果权限列表具有add 方法 添加postinsert
                    if($v['aname']=="getadd"){
                        $nodelist[$v['mname']][]="postinsert";
                    }
                    //如果权限列表具有edit方法 添加postupdate
                    if($v['aname']=="getedit"){
                        $nodelist[$v['mname']][]="postupdate";
                    }
                }
                //把当前登录用户所具有的权限信息 存储在session里
                Session::set('nodelist',$nodelist);
                
                //跳转到后台首页
    			$this->success("登录成功","/admin/index");	
    		}else{
    			$this->error("用户名或密码不正确","/adminlogin/login");
    		}
    	}else{
    		$this->error("验证码输入有误,重新输入","/adminlogin/login");
    	}
    }
    public function getloginout(){
    	Session::delete('islogin');
        Session::delete('username');
    	Session::delete('usersid');
    	$this->success('退出成功','/adminlogin/login');
    }
}
